package com.system.controller;


import com.system.entity.User;
import com.system.service.UserService;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 添加用户信息
     *
     */
     @RequestMapping(value="/addUser",method =RequestMethod.GET)
     public ModelAndView toaddUser(ModelAndView model){
         //通过ModelAndView中的方法跳转到userAdd.jsp页面
         model.setViewName("user/userAdd");
         return model;
     }

    /**
     * 添加用户表单处理方法
     * @param user
     * @return
     */
    @RequestMapping(value="/addUser")
    @ResponseBody
    public String addUser(User user){
        try {
           userService.addUser(user);
           return "ok";

        } catch (Exception e) {
           e.printStackTrace();
        }
     return null;

    }

    /**
     * 根据id查询用户信息方法
     *
     * @param user
     *  @return
     */

    @RequestMapping(value = "/findById",method = RequestMethod.POST)
    @ResponseBody
    public User findById(@RequestBody User user) {
        User user_info = userService.findById(user.getUid());
            return user_info;

    }

    /**
     * 更新用户信息的方法
     *
     * @param user
     * @param model
     * @return
     */
    @RequestMapping(value = "/updateUser")
    public String updateUser(User user, Model model) {
        try {
           userService.updateUser(user);
            model.addAttribute("message", "更新客户信息成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "user/userlist";
    }


    /**
     * 通过编号删除用户
     *
     */
    @RequestMapping(value = "/deleteUser")
    public String deleteUser(@RequestParam("id") Integer id, Model model) {
        try {
            userService.deleteUser(id);
            model.addAttribute("message", "删除客户信息成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "user/userlist";
    }

    /**
     * 分页查询
     *
     * @param user 查询条件
     * @param pageCode 当前页
     * @param pageSize 每页显示的记录数
     * @return
     */
    @RequestMapping("/findByPage")
    public String findByPage(User user,
                             @RequestParam(value = "pageCode", required = false, defaultValue = "1") int pageCode,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "4") int pageSize,
                             Model model) {
        // 回显数据
        model.addAttribute("page", userService.findByPage(user, pageCode, pageSize));
        return "user/userlist";
    }

    /**
     * 通过id组批量删除用户
     * @param ids
     * @return
     */
    @RequestMapping("deleteByIds")
    @ResponseBody
    public String deleteUserByIds(Integer[] ids){
        try{
            //批量删除
            userService.deleteUserByIds(ids);
            return "ok";
        }
        catch (Exception e){
            return "error";
        }

    }

    /**
     * 验证用户ID号是否存在
     *
     */
    @ResponseBody
    @RequestMapping(value="/userValidate", method=RequestMethod.POST)
    public String userValidate(User user) {

       try {
       User result=userService.userValidate(user);
        if(result!=null){
            return "ok";
        }
        else{
            return "no";
        }

       }
       catch (Exception e){
           e.printStackTrace();
       }
         return null;
       }


}
